package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.PlaceInfoMapper;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.notice.NoticeAddRequest;
import com.lnking.lnkingplace.model.request.notice.NoticeSearchRequest;
import com.lnking.lnkingplace.model.request.notice.NoticeUpdateRequest;
import com.lnking.lnkingplace.model.request.suggest.SuggestAddRequest;
import com.lnking.lnkingplace.model.request.suggest.SuggestSearchRequest;
import com.lnking.lnkingplace.model.request.suggest.SuggestUpdateRequest;
import com.lnking.lnkingplace.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-19
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "公告通知")
public class NoticeController {

    @Resource
    INoticeService noticeService;
    @Resource
    UserUtil userUtil;
    @Resource
    PlaceInfoMapper placeInfoMapper;

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public ReturnResult getList(@RequestBody NoticeSearchRequest params) {
        params.setChannel(userUtil.getUser().getChannel());
        //人员类型
        String userType = userUtil.getUser().getType();
        //从业人员查看全员通知和精准通知
        if(userType.equals(UserType.USER)){
            params.setPreciseId(userUtil.getUser().getId());
        }
        //场所人员查询3个通知
        else if(userType.equals(UserType.LOCATION)){
            String placeId = userUtil.getUser().getPlaceId();
            String placeType = placeInfoMapper.getPlaceType(placeId);
            params.setPreciseId(placeId);
            params.setTradeType(placeType);
        }
        //监管人员查看
        else if(userType.equals(UserType.SUPERVISE)){
            params.setAllFlag("sup");
        }
        return ReturnResult.success(noticeService.pageList(params));
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public ReturnResult add(@RequestBody @Valid NoticeAddRequest params)throws Exception{
        noticeService.add(params);
        return ReturnResult.success();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ReturnResult update(@RequestBody @Valid NoticeUpdateRequest params)throws Exception{
        noticeService.update(params);
        return ReturnResult.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        noticeService.removeById(id);
        return ReturnResult.success();
    }

    @ApiOperation("查询详情")
    @GetMapping("get-details")
    public ReturnResult getDetails(String id){
        return ReturnResult.success(noticeService.getOne(id));
    }

    @ApiOperation("已读")
    @GetMapping("read")
    public ReturnResult read(String id){
        noticeService.read(id);
        return ReturnResult.success();
    }

    @ApiOperation("查询已读/未读")
    @GetMapping("query-read-info")
    public ReturnResult queryReadInfo(String id){
        return ReturnResult.success(noticeService.queryReadInfo(id));
    }
}
